<template>
  <el-dialog v-model="visible" title="个人资料" width="500px" @close="handleClose">
    <el-form :model="form" label-width="80px">
      <el-form-item label="头像">
        <el-upload class="avatar-uploader" action="#" :show-file-list="false" :auto-upload="false"
          :on-change="handleAvatarChange">
          <el-avatar :size="80" :src="form.avatar">
            <el-icon>
              <User />
            </el-icon>
          </el-avatar>
          <div class="upload-tip">点击更换头像</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickname" placeholder="请输入昵称" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="个人简介">
        <el-input v-model="form.bio" type="textarea" :rows="3" placeholder="介绍一下自己吧" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSave">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { User } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user/index.js'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:modelValue', 'save'])

const userStore = useUserStore()
const visible = ref(props.modelValue)

const form = reactive({
  avatar: '',
  username: '',
  nickname: '',
  email: '',
  phone: '',
  bio: ''
})

watch(
  () => props.modelValue,
  (val) => {
    visible.value = val
    if (val) {
      loadUserInfo()
    }
  }
)

watch(visible, (val) => {
  emit('update:modelValue', val)
})

const loadUserInfo = () => {
  Object.assign(form, {
    avatar: userStore.userInfo?.avatar || '',
    username: userStore.userInfo?.username || '',
    nickname: userStore.userInfo?.nickname || '',
    email: userStore.userInfo?.email || '',
    phone: userStore.userInfo?.phone || '',
    bio: userStore.userInfo?.bio || ''
  })
}

const handleAvatarChange = (file) => {
  const reader = new FileReader()
  reader.onload = (e) => {
    form.avatar = e.target.result
  }
  reader.readAsDataURL(file.raw)
}

const handleSave = () => {
  console.log('保存个人资料:', form)
  emit('save', form)
  ElMessage.success('个人资料保存成功')
  handleClose()
}

const handleClose = () => {
  visible.value = false
}
</script>

<style scoped lang="less">
.avatar-uploader {
  text-align: center;

  .upload-tip {
    margin-top: 8px;
    font-size: 12px;
    color: #999;
  }

  :deep(.el-upload) {
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      opacity: 0.8;
    }
  }
}
</style>
